/*jslint maxlen:150*/
/*global define:false,K:false*/
/**
 * @class Dialog 继承于Panel，实现开心最常用的Dialog功能
 */
define( 'core/panel/Dialog', [ 'jQuery', 'core/panel/Panel', 'core/panel/Mask' ], function( require ) {
    var $ = require( 'jQuery' ),
        Panel = require( 'core/panel/Panel' ),
        Mask = require( 'core/panel/Mask' ),
        mix = K.mix,
        extend = K.extend;

    var Dialog = extend( function( opts ) {
        this._initDialog( opts );
    }, Panel );

    mix( Dialog.prototype, {
        /*
			可用的选项包括
			{
				top:		//坐标					(默认为0)
				left:		//坐标					(默认为0)
				height:		//高度					(默认为0)
				width:		//宽度					(默认为0)
				atCenter:	//是否居中				(默认为true)
				useIframe:	//是否使用iframe遮挡	(默认自动判断)
				useMask:	//是否使用遮罩			(默认使用)
				useHead:	//是否使用头部			(默认使用)
				useFoot:	//是否使用底部			(默认使用)
				title:		//Dialog标题			(默认为空)
				className:	//Dialog样式名			(默认为panel-t1)
				content:	//Dialog内容			(默认为空)
				foot:		//Dialog底栏内容		(默认为空)
                autoLayout: //是否自适应高度和宽度  (默认为否)
                maskOption: //Mask选项              (默认为空)
			}
		 */
        _initDialog: function( opts ) {
            this.useMask = true;
            this.useHead = true;
            this.useFoot = true;

            this.title = '';
            this.content = '';
            this.foot = '';
            this.className = 'panel-t1';

            //点击Mask是否关闭窗口
            this.clickMaskClose = false;

            //阴影宽度(TOP与BOTTOM采用阴影,Left和Right为Border)
            this.sdBorderWidthTB = 8;
            this.sdBorderWidthLR = 7;
            this.hdHeight = 25;

            mix( this, opts );

            Dialog.$super.call( this, opts );

            this.addClass( this.className );

            this._createDialog( );

            if ( this.useMask ) {
                this._createMask( );
            }

            this._bindEvents( );

            if ( this.title ) {
                this.setTitle( this.title );
            }

            if ( !this.useHead ) {
                this._hd.hide( );
            }

            if ( this.content ) {
                this.setContent( this.content );
            }

            if ( this.foot ) {
                this.setFooter( this.foot );
            }

            if ( !this.useFoot ) {
                this._ft.hide( );
            }

            this.layout( );
        },
        /*
         * 创建Dialog结构
         */
        /*
			<div class="panel panel-t1" style="top: 48px; left: 100px; width: 520px; height: 200px;">
				<div class="panel-content">
					<div class="hd">
						<h3>这里是标题</h3>
					</div>
					<div class="bd">
					</div>
					<div class="ft"></div>
				</div>
				<div class="sd">
					<div class="sdconner"><div></div></div>
					<div class="rect">
					</div>
					<div class="sdconner sdconner_b"><div></div></div>
				</div>
				<div class="close">×</div>
			</div>
		*/
        _createDialog: function( ) {
            //创建内容区域
            this._hd = $( '<div class="hd"/>' );
            this._bd = $( '<div class="bd"/>' );
            this._ft = $( '<div class="ft"/>' );

            this.appendToContent( this._hd );
            this.appendToContent( this._bd );
            this.appendToContent( this._ft );

            //创建shadow
            this._sd = $( '<div class="sd"/>' );
            this.appendToPanel( this._sd );
            this._sd.html(
                '<div class="sdconner"><div></div></div>' +
                '<div class="rect"></div>' +
                '<div class="sdconner sdconner_b"><div></div></div>'
            );

            //创建close按钮
            this._close = $( '<div class="close">×</div>' );
            this.appendToPanel( this._close );
        },
        /*
         * 创建Mask
         */
        _createMask: function( ) {
            var maskOpt = {};

			if( this.maskOption ){
				K.mix( maskOpt, this.maskOption );
			}

            this._mark = new Mask( maskOpt );
            this._mark.follow( this );


            if ( this.clickMaskClose ) {
                var ins = this;
                this._mark.on( 'activate', function( ) {
                    ins.hide( );
                } );
            }

            //将Panel嵌入Mask（特殊应用）
            if( this.embedMask ){
				this.getPanel().appendTo( this._mark.getPanel() );
			}
        },
        /*
         * 绑定事件
         */
        _bindEvents: function( ) {
            var ins = this;
            //Close
            this._close.click( function( ) {
                ins.hide( );
            } );
        },
        /**
         * 设置Dialog的坐标和尺寸
         *
         * @method setRect
         * @param  {number} left	坐标
         * @param  {number} top		坐标
         * @param  {number} width	宽度
         * @param  {number} height	高度
         * @return {Dialog object}
         */
        setRect: function( rect ) {
            var w = rect.width,
                h = rect.height;

            this._panel.offset( rect );
            if ( typeof( w ) != 'undefined' && !this.autoLayout ) {
                rect.width = this.fixWidth( w );
            }
            if ( typeof( h ) != 'undefined' && !this.autoLayout ) {
                rect.height = this.fixHeight( h );
            }

            Dialog.$super.prototype.setRect.call( this, rect );

            return this;
        },
        /**
         * 设置Dialog的尺寸
         *
         * @method setSize
         * @param  {number} w宽度
         * @param  {number} h高度
         * @param  {boolean} atCenter 设置Size后是否自动居中（默认不居中）
         * @return {Dialog object}
         */
        setSize: function( w, h, atCenter ) {
            if( !this.autoLayout ) {
                w = this.fixWidth( w );
                h = this.fixHeight( h );
            }
            Dialog.$super.prototype.setSize.call( this, w, h );
            this.layout( !! atCenter );
            return this;
        },
        setFullscreen: function( ) {
            Dialog.$super.prototype.setFullscreen.call( this );
            this.layout( );
            return this;
        },
        /**
         * 设置Dialog标题
         * @param {String} title Dialog标题
         */
        setTitle: function( title ) {
            this.title = title;
            this._hd.html( '<h3>' + title + '</h3>' );
            return this;
        },
        /**
         * 设置Dialog内容
         * @param {String|Element} content Dialog内容;如果是字符串则直接当做内容，如果是元素则直接append。
         */
        setContent: function( content ) {
            if ( $.type( content ) == 'string' ) {
                this._bd.html( content );
            }
            else {
                this._bd.empty( );
                this._bd.append( $( content ) );
            }
        },
        /**
         * 设置底栏内容
         * @param {String} footer Dialog底栏内容
         */
        setFooter: function( footer ) {
            this.foot = footer;
            this._ft.html( footer );
            return this;
        },
        /**
         * 计算高度等操作
         * @param {boolean} atCenter 是否居中
         */
        layout: function( atCenter ) {
            if( this.autoLayout ) {
                return;
            }

            //设置bd的高度
            var rect = this.getRect( ),
                noRectHeight =
                    this._sd.find( '.sdconner' ).eq( 0 ).height( ) +
                    this._sd.find( '.sdconner' ).eq( 1 ).height( ) +
                    3;

            if ( this.useHead ) {
                noRectHeight += this._hd.height( );
            }

            if ( this.useFoot ) {
                noRectHeight += this._ft.height( );
            }

            this._bd.css( 'height', rect.height - noRectHeight );

            //调整阴影高度(与样式相关，目前没有找到好的纯样式解决方案，待替换)
            var imgHeight =
                this._sd.find( '.sdconner' ).eq( 0 ).height( ) +
                this._sd.find( '.sdconner' ).eq( 1 ).height( );

            this._sd.find( '.rect' ).css( 'height', rect.height - imgHeight - 1 );

            if ( this.atCenter && atCenter !== false ) {
                this.center( );
            }
        },
        //传入内容的宽度，返回的是Dialog的宽度
        fixWidth: function( w ) {
            return w + this.sdBorderWidthLR * 2 + 2;
        },
        //传入内容的高度，返回的是Dialog的高度
        fixHeight: function( h ) {
            return h + this.sdBorderWidthTB * 2 + 3;
        },
        getMask: function( ) {
            return this._mark;
        },
	getClose: function( ) {
	    return this._close;
	},
        dispose: function( ) {
            Dialog.$super.prototype.dispose.call( this );
            if( this._mark ) {
                this._mark.dispose();
            }
        }
    } );

    return Dialog;
} );
